#include <iostream>
#include <cstring>
#include <windows.h>
using namespace std;

// 定义MAC地址长度为6字节
#define MAC_ADDRESS_LENGTH 6

// 定义MAC地址类型
typedef unsigned char MAC_address[MAC_ADDRESS_LENGTH];

// 定义以太网帧结构体
struct EthernetFrame {
    MAC_address destination;  // 目的MAC地址
    MAC_address source;       // 源MAC地址
    unsigned short type;      // 类型字段
};

// 定义本机MAC地址
MAC_address this_mac_address = {0x00, 0x50, 0x56, 0xC0, 0x00, 0x01};

// 判断MAC地址是否匹配的函数
int mac_address_match(const struct EthernetFrame *frame) {
    // 检查广播地址 (FF:FF:FF:FF:FF:FF)
    bool is_broadcast = true;
    for(int i = 0; i < MAC_ADDRESS_LENGTH; i++) {
        if(frame->destination[i] != 0xFF) {
            is_broadcast = false;
            break;
        }
    }
    if(is_broadcast) return 1;

    // 检查多播地址 (第一位为1)
    if(frame->destination[0] & 0x01) return 1;

    // 检查单播地址是否匹配本机地址
    bool is_unicast_match = true;
    for(int i = 0; i < MAC_ADDRESS_LENGTH; i++) {
        if(frame->destination[i] != this_mac_address[i]) {
            is_unicast_match = false;
            break;
        }
    }
    if(is_unicast_match) return 1;

    // 都不匹配则返回0
    return 0;
}

// 测试函数
int main() {

    SetConsoleOutputCP(CP_UTF8);

    EthernetFrame test_frame;

    // 测试广播地址
    memset(test_frame.destination, 0xFF, MAC_ADDRESS_LENGTH);
    cout << "广播地址测试: " << mac_address_match(&test_frame) << endl;

    // 测试多播地址
    test_frame.destination[0] = 0x01; // 多播地址第一位为1
    cout << "多播地址测试: " << mac_address_match(&test_frame) << endl;

    // 测试本机地址
    memcpy(test_frame.destination, this_mac_address, MAC_ADDRESS_LENGTH);
    cout << "本机地址测试: " << mac_address_match(&test_frame) << endl;

    // 测试不匹配地址
    MAC_address other_mac = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55};
    memcpy(test_frame.destination, other_mac, MAC_ADDRESS_LENGTH);
    cout << "不匹配地址测试: " << mac_address_match(&test_frame) << endl;

    return 0;
}